package com.df.bpm.utils.businessViewerV2.business;

import com.df.bpm.utils.businessViewerV2.Main;
import com.df.bpm.utils.businessViewerV2.entity.C_entity;
import com.df.bpm.utils.businessViewerV2.entity.M_entityDto;
import com.df.bpm.utils.businessViewerV2.res.V_A;
import com.df.common.utils.CommonUtils;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class V_businessDto4Search extends V_A<M_businessDto4Search> {

    Integer requirementId;
    public V_businessDto4Search(Integer requirementId) {
        this.requirementId=requirementId;
    }

    @Override
    public List<String> getColNames() {
        return Arrays.asList("ID", "序号", "业务简述","担当者","输入物","输出物");
    }

    @Override
    public List<String> getKeyCols() {
        return Arrays.asList("业务简述","担当者","输入物","输出物");
    }

    @Override
    public void hideCol(JTable iTable) {
        Main.hideCol(iTable,0);
    }

    @Override
    public void setDialogSize(int width, int height) {
        jd.setSize(50*unit, 40*unit);
    }

    @Override
    public List<Integer> colWidthSetting() {
        return new ArrayList<>(Arrays.asList(1,3,18,7,10,10));
    }

    @Override
    public Object[] getRowValues(M_businessDto4Search row) {
        return new Object[]{row.getBusinessInformationIndex(),-1,row.getBusinessDesc(),row.getDirectorName(),row.getInputName(),row.getOutputName()};
    }

    @Override
    public MouseListener mouseAdapter4Table(JTable iTable, List<M_businessDto4Search> list) {
        return new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row = iTable.rowAtPoint(e.getPoint());
                int col = iTable.columnAtPoint(e.getPoint());
                if (row >= 0 ) {
                    if (e.getClickCount() == 2) {// 处理双击事件
//                        System.out.println("Row " + row + "& Col "+col+" double clicked");
                        String entityID=""+iTable.getValueAt(row, 0);
                        if(col==1 && CommonUtils.varIsNotBlank(entityID)){//不为空的双击代表选中
                            selectResult=list.get(row);
                            jd.dispose();//选好窗口消失
                        }else{
                            if(col==2){//业务简述
                                String oldValue=list.get(row).getBusinessDesc();
                                String tips="请输入业务简述：";
                                String newValue= (String) JOptionPane.showInputDialog(null, tips, "业务简述", JOptionPane.PLAIN_MESSAGE, null, null, oldValue);
                                if(newValue!=null && !newValue.equals(oldValue)){
                                    list.get(row).setBusinessDesc(newValue);
                                    iTable.setValueAt(newValue, row, col);
                                    uploadBusiness(list.get(row));
                                }
                            }else if(col==3){//担当者
                                Integer oldDirector=list.get(row).getEntityID();
                                M_entityDto newDirector = C_entity.selectOne();
                                if(newDirector!=null && !Objects.equals(newDirector.getBusinessEntityIndex(), oldDirector)){
                                    list.get(row).setEntityID(newDirector.getBusinessEntityIndex());
                                    list.get(row).setDirectorName(newDirector.getName());
                                    iTable.setValueAt(newDirector.getName(), row, col);
                                    uploadBusiness(list.get(row));
                                }
                            }

                        }

                    }
                }
            }
        };
    }

    private void uploadBusiness(M_businessDto4Search temp) {
        if(temp.getBusinessDesc()!=null && temp.getEntityID()!=null){//没填全不上传
            if("-".equals(temp.getBusinessDesc()) ){//删除
                if(temp.getBusinessInformationIndex()!=null) {
                    C_business.deleteBusiness(temp.getBusinessInformationIndex());
                }else{
                    JOptionPane.showMessageDialog(null, "业务简述不能为：-", "上传失败", JOptionPane.ERROR_MESSAGE);
                }
            }else{//增、改
                C_business.updateBusiness(temp, requirementId);
            }
            try {
                search();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e.getMessage(), "查询失败", JOptionPane.ERROR_MESSAGE);
                throw new RuntimeException(e);
            }
        }
    }

    @Override
    public M_businessDto4Search getEmptyRow() {
        return new M_businessDto4Search();
    }

    @Override
    public void search(List<JTextField> fields)  {
        M_businessDto4Search temp=new M_businessDto4Search();
        //"业务简述","担当者","输入物","输出物"
        if (CommonUtils.varIsNotBlank(fields.get(0).getText())) {
            temp.setBusinessDesc(fields.get(0).getText());
        }
        if (CommonUtils.varIsNotBlank(fields.get(1).getText())) {
            temp.setDirectorName(fields.get(1).getText());
        }
        if (CommonUtils.varIsNotBlank(fields.get(2).getText())) {
            temp.setInputName(fields.get(2).getText());
        }
        if (CommonUtils.varIsNotBlank(fields.get(3).getText())) {
            temp.setOutputName(fields.get(3).getText());
        }
        List<M_businessDto4Search> results= C_business.getBusinessDto4Search(temp);
        this.putModel(results);
    }


}
